package com.jdd.modules.dashboard.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.jdd.common.result.Result;
import com.jdd.common.util.LoginUserUtils;
import com.jdd.dao.service.TProjectService;
import com.jdd.modules.dashboard.dto.ThroughDataDTO;
import com.jdd.modules.dashboard.vo.ThroughTypeVO;
import com.jdd.modules.device.entity.TDevice;
import com.jdd.modules.device.service.TDeviceService;
import com.jdd.modules.dooropeningrecord.mapper.DoorOpeningRecordMapper;
import com.jdd.modules.through.service.IThroughService;
import com.jdd.modules.userstaff.entity.User;
import com.jdd.modules.userstaff.service.IUserService;
import com.jdd.modules.visitorLog.entity.VisitorLog;
import com.jdd.modules.visitorLog.service.IVisitorLogService;
import com.jdd.modules.visitors.entity.Visitor;
import com.jdd.modules.visitors.service.IVisitorService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("dashboard/analysis")
@Api("首页")
public class DashboardController {

    @Autowired
    private TProjectService projectService;
    @Autowired
    private TDeviceService deviceService;
    @Autowired
    private IVisitorService visitorService;
    @Resource
    private IVisitorLogService visitorLogService;
    @Autowired
    private IUserService userService;
    @Resource
    private IUserService itUserService;

    @Resource
    private DoorOpeningRecordMapper doorOpeningRecordMapper;
    @Resource
    private IThroughService throughService;

    /**
     * 基本信息
     * @param map
     * @return
     */
    @PostMapping(value = "/AnalysisData")
    public Result<Map<String, Object>> analysisData(@RequestBody Map<String, Object> map){
        Result<Map<String, Object>> result = new Result<>();
        Map<String, Object> jsonArray = new HashMap<>();
        if(LoginUserUtils.getProjectCodeList().isEmpty())
        {
            jsonArray.put("ProjectNumber", 0);
            jsonArray.put("DeviceNumber", 0);
            //通道数
            jsonArray.put("PassagewayNumber", 0);
            //可通行人数
            jsonArray.put("WalkableNumber", 0);
            result.setData(jsonArray);
            result.setCode(Result.SUCCESS_CODE);
            result.setMsg("success");
            return result;
        }
        //项目数
        jsonArray.put("ProjectNumber", LoginUserUtils.getProjectCodeList().size());
        LambdaQueryWrapper<TDevice> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(TDevice::getProjectId,LoginUserUtils.getProjectCodeList());
        //门禁数
        int deviceNumber = deviceService.count(queryWrapper);
        jsonArray.put("DeviceNumber", deviceNumber);
        //通道数
        jsonArray.put("PassagewayNumber", deviceNumber);
        int people = itUserService.count();
        //可通行人数
        jsonArray.put("WalkableNumber", people);

        result.setData(jsonArray);
        result.setCode(Result.SUCCESS_CODE);
        result.setMsg("success");
        return result;
    }

    /**
     * 被访人员top5
     * @return
     */
    @GetMapping("/getVisitTop")
    public Result<List<Visitor>> getVisitTop(){
        Result<List<Visitor>> result = new Result<>();
        List<Visitor> visit = visitorService.getVisitTop(LoginUserUtils.getProjectCodeList());
        for(Visitor tVisitor :visit)
        {
            User user = userService.getById(tVisitor.getUserId());
            if(user!=null)
            {
                tVisitor.setUserName(user.getUserName());
            }
            if(StringUtils.isBlank(tVisitor.getUserName()))
            {
                tVisitor.setUserName("");
            }
        }
        result.setCode(Result.SUCCESS_CODE);
        result.setData(visit);
        result.setMsg("success");
        return result;
    }

    /**
     * 来访人数统计
     * @return
     */
    @GetMapping("/getVisitNum")
    public Result<List<VisitorLog>> getVisitNum(){
        Result<List<VisitorLog>> result = new Result<>();
        List<VisitorLog> visitNum = visitorLogService.getVisitNum(LoginUserUtils.getProjectCodeList());
        result.setCode(Result.SUCCESS_CODE);
        result.setData(visitNum);
        result.setMsg("success");
        return result;
    }
    /**
     * 通行类型分部查询
     * */
    @GetMapping("/getThroughType")
    public Result<List<ThroughTypeVO>> getThroughType(){
        Result<List<ThroughTypeVO>> result = new Result<>();
        List<ThroughTypeVO> throughTypeVoList =doorOpeningRecordMapper.getThroughTypeVoList(LoginUserUtils.getProjectCodeList());
        result.setData(throughTypeVoList);
        result.setCode(200);
        return result;
    }
    /**
     * 通行类型分部查询
     * */
    @GetMapping("/getThroughNumber")
    public Result<List<ThroughTypeVO>> getThroughNumber(){
        Result<List<ThroughTypeVO>> result = new Result<>();
        List<ThroughTypeVO> throughTypeVoList =doorOpeningRecordMapper.getThroughTypeVoList(LoginUserUtils.getProjectCodeList());
        result.setData(throughTypeVoList);
        result.setCode(200);
        return result;
    }
    @GetMapping("/getThroughData")
    public Result<?> getThroughData(){
        Result<List<ThroughDataDTO>> result = new Result<>();
        result.setData(throughService.getThroughData(LoginUserUtils.getProjectCodeList()));
        result.setCode(200);
        return result;
    }
}
